{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# OpenCV基础使用\n",
    "## 第一章 入门及处理基础\n",
    "1. opencv基本的读取图像，显示图像，保存图像\n",
    "   - 读取图像 imread()\n",
    "   - 显示图像 imshow()\n",
    "   - 保存图像 imwrite()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import cv2\n",
    "# 读取图像\n",
    "i = cv2.imread(\"./runoob.jpg\")\n",
    "# 显示图像\n",
    "cv2.imshow(\"Demo\",i)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()\n",
    "# 保存图像 保存成功返回True\n",
    "cv2.imwrite(\"./runoob2.jpg\",i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. 像素处理\n",
    "   - item可以直接访问像素点，itemset可以修改像素点的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "# 读取图像\n",
    "i = cv2.imread(\"./runoob.jpg\")\n",
    "\n",
    "\n",
    "# 像素处理\n",
    "'''\n",
    "i[:,100:200,0] = 255\n",
    "或者\n",
    "# item可以直接访问像素点，itemset可以修改像素点的值\n",
    "# i.itemset((100:300,100:300,0),255)\n",
    "# print(i.item(100,100,2))\n",
    "# i.itemset((100,100,2),255)\n",
    "# print(i.item(100,100,2))\n",
    "'''\n",
    "\n",
    "i[:,100:200,0] = 255\n",
    "\n",
    "cv2.imshow(\"Demo\",i)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. 图像属性\n",
    "   - 图像形状 shape \n",
    "       - 彩色图像  返回行数、列数、通道数\n",
    "       - 灰色图像  返回行数、列数\n",
    "   - 像素数目 size\n",
    "       - 彩色图像  行数 * 列数 * 通道数\n",
    "       - 灰色图像  行数 * 列数\n",
    "   - 图像类型 dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(277, 300, 3)\n",
      "249300\n",
      "uint8\n"
     ]
    }
   ],
   "source": [
    "import cv2\n",
    "# 读取图像\n",
    "i = cv2.imread(\"./runoob.jpg\")\n",
    "\n",
    "# 图像形状\n",
    "print(i.shape)\n",
    "# 像素数目\n",
    "print(i.size)\n",
    "# 图像类型\n",
    "print(i.dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4. 图像ROI 即：图像感兴趣区域"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(277, 300)\n"
     ]
    }
   ],
   "source": [
    "import cv2\n",
    "img = cv2.imread(\"./runoob.jpg\",cv2.IMREAD_GRAYSCALE)\n",
    "print(img.shape)\n",
    "cry = img[10:200,150:300]\n",
    "cv2.imshow(\"origin\", i)\n",
    "cv2.imshow(\"cry\", cry)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5. 通道的拆分（split）、合并（merge）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "\n",
    "img = cv2.imread(\"./runoob.jpg\")\n",
    "# 第一种方式拆分\n",
    "r = img[:,:,2]\n",
    "g = img[:,:,1]\n",
    "b = img[:,:,0]\n",
    "# 第二种方式拆分 \n",
    "b, g, r = cv2.split(img)\n",
    "# 或者\n",
    "r = cv2.split(img)[2]\n",
    "g = cv2.split(img)[1]\n",
    "b = cv2.split(img)[0]\n",
    "rows,cols,none = img.shape\n",
    "\n",
    "# 合并 merge方法 [务必注意通道合并顺序，OpenCV是bgr顺序]\n",
    "new_photo = cv2.merge([b, g, r])\n",
    "cv2.imshow(\"new_photo\", new_photo)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第二章 图像运算\n",
    "1. 图像加法 注意：参与运算的图像的大小和类型必须一致\n",
    "   - 使用cv2.add 方法 结果 = cv2.add(图像1,图像2)  也称之为饱和运算\n",
    "       - 像素值 <= 255 图像1 + 图像2\n",
    "       - 像素值 >255 结果取255\n",
    "   - 使用numpy方法直接相加 结果 = 图像1 + 图像2\n",
    "       - 像素值 <= 255 图像1 + 图像2\n",
    "       - 像素值 >255 结果对255取模（就是取余数）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "img1 = cv2.imread(\"./runoob.jpg\", cv2.IMREAD_GRAYSCALE)\n",
    "img2 = img1\n",
    "# 使用numpy方法\n",
    "img3 = img1 + img2\n",
    "# 使用cv2.add方法\n",
    "img4 = cv2.add(img1, img2)\n",
    "cv2.imshow(\"img3\", img3)\n",
    "cv2.imshow(\"img4\", img4)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. 图像融合 注意：参与运算的图像的大小和类型必须一致\n",
    "    - 结果图像 = 图像1 * 系数1 + 图像2 * 系数2 + 亮度调节量\n",
    "    - addWeighted\n",
    "        - cv2.addWeighted(src1,k1,scr2,k2,gamma)\n",
    "        参数gamma不可以省略"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(77, 300, 3)\n",
      "(77, 300, 3)\n"
     ]
    }
   ],
   "source": [
    "import cv2\n",
    "img1 = cv2.imread(\"./runoob.jpg\")\n",
    "img2 = cv2.imread(\"./runoob2.png\")\n",
    "img1_split = img1[:77,:]\n",
    "img2_split = img2[:77,:300]\n",
    "# 使用cv2.addWeighted方法\n",
    "img4 = cv2.addWeighted(img1_split,0.3, img2_split, 0.7, 0)\n",
    "cv2.imshow(\"img4\", img4)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第三章 类型转换\n",
    "    使用cv2.cvtColor()函数实现类型转化\n",
    "   - result = cv2.cvtColor(src, code[, dstCn])\n",
    "        - src 表示原图像\n",
    "        - code 表示色彩转换码\n",
    "        - dstCn 表示目标的通道数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "img1 = cv2.imread(\"./runoob.jpg\", cv2.IMREAD_UNCHANGED)\n",
    "img2 = cv2.cvtColor(img1,cv2.COLOR_BGR2RGB)\n",
    "cv2.imshow(\"img2\", img2)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第四章 几何变换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
